package com.siam.package_rider.service_impl;

import java.math.BigDecimal;
import java.util.*;

import com.siam.package_common.constant.Quantity;
import com.siam.package_merchant.entity.Merchant;
import com.siam.package_rider.cache.RiderSessionManager;
import com.siam.package_rider.entity.TbRider;
import com.siam.package_rider.util.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.siam.package_rider.entity.TbRiderBill;
import com.siam.package_rider.model.param.TbRiderBillParam;
import com.siam.package_rider.mapper.TbRiderBillMapper;
import com.siam.package_rider.service.TbRiderBillService;

/**
 * ---------------------------
 * 骑手账单表 (TbRiderBillServiceImpl)         
 * ---------------------------
 * 作者：  高品
 * 时间：  2025-03-29 18:55:18
 * ---------------------------
 */
@Service
public class TbRiderBillServiceImpl extends ServiceImpl<TbRiderBillMapper, TbRiderBill> implements TbRiderBillService {

	@Autowired
	private TbRiderBillMapper tbRiderBillMapper;

	@Autowired
	private RiderSessionManager riderSessionManager;

	@Override
	public void insert(TbRiderBillParam param) {
		TbRiderBill record = new TbRiderBill();
		BeanUtils.copyProperties(param, record);
        tbRiderBillMapper.insert(record);
	}

	@Override
	public void delete(Long id) {
		tbRiderBillMapper.deleteById(id);
	}

	@Override
	public void batchDelete(List<Long> idList) {
		tbRiderBillMapper.deleteBatchIds(idList);
	}

	@Override
	public void update(TbRiderBillParam param) {
		TbRiderBill record = new TbRiderBill();
		BeanUtils.copyProperties(param, record);
		tbRiderBillMapper.updateById(record);
	}

	@Override
	public Page<TbRiderBill> page(TbRiderBillParam param) {
		// 如果没有指定货币类型，则默认只查余额类型
		if(param.getCoinType() == null){
			param.setCoinTypeList(Arrays.asList(Quantity.INT_1));
		}
        Page<TbRiderBill> page = tbRiderBillMapper.page(new Page(param.getPageNo(), param.getPageSize()), param);
        return page;
	}

	@Override
	public TbRiderBill findById(Long id) {
		return tbRiderBillMapper.selectById(id);
	}

	@Override
	public Map<String, Object> statisticalAmount(TbRiderBillParam param) {
		TbRider loginTbRider = riderSessionManager.getSession(TokenUtil.getToken());
		if(loginTbRider != null){
			param.setRiderId(loginTbRider.getId());
		}

		BigDecimal incomeAmount = tbRiderBillMapper.statisticalAmountByIncome(param);
		BigDecimal expendAmount = tbRiderBillMapper.statisticalAmountByExpend(param);

		Map<String, Object> map = new HashMap<>();
		map.put("incomeAmount", incomeAmount);
		map.put("expendAmount", expendAmount);
		return map;
	}

	@Override
	public BigDecimal selectSumNumber(TbRiderBillParam param, Date startTime, Date endTime) {
		return tbRiderBillMapper.selectSumNumber(param, startTime, endTime);
	}
}